

$(function() {
	
	initTable();
	
	$('#searchUser').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
//	//绑定机构下拉列表
//	bindOrg('org');

	//绑定机构下拉列表
	bindOrg1('orgcode');
	
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			$('#orgcode').val(data.orgname);
			$('#orgcode').attr('data', data.orgcode);
		}
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'user/list.do',
	    pagination: true, //分页
	    dataType: "json",
//        contentType: "application/json",
        pageList: [10],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
            		pageNum: (params.offset / params.limit + 1).toString(),
                	pageSize: params.limit.toString(),
                	username: $('#username').val(),
                	orgcode : $('#orgcode').attr('data')
                };
        },
	    columns: [{  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'username',
            align : 'center',
	        title: '用户名称'
	    }, {
	        field: 'orgname',
            align : 'center',
	        title: '所属机构'
	    },  {
	        field: 'deptname',
            align : 'center',
	        title: '所属部门'
	    },  {
            field: 'operate',
            title: '选择用户',
            align: 'center',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
        '<button type="button" class="SeleteUser btn btn-success  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>选择用户</button>'
    ].join('');
}

window.operateEvents = {
    'click .SeleteUser': function (e, value, row, index) {
    	window.parent.putUser(row);
    }
 };


function bindOrg(obj){

	$.ajax({ 
		method: 'post',
		url: projectPath + "org/getOrgControl.do",
		data:JSON.stringify({"porgcode":"000000"}),
		dataType: "json",
		contentType: "application/json",
		success: function(data){
		    var cityPicker = new IIInsomniaCityPicker({
		        data: data,
		        target: '#' + obj,
		        callback: function(id){
		        	var id = $('#' + obj).attr("data");
		        	var name = $('#' + obj).val();   	
		        	$('#orgcode').val(id);
		        	$('#orgname').val(name);  	
		        }
		    });
		    cityPicker.init();
		}
	});
}
